package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.mapper.*;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.SalesStatisticVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IIndexService;
import org.apache.commons.collections4.list.TreeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class IIndexServiceImpl implements IIndexService {
    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ReportMapper reportMpper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SalesStatisticVo> selectGetVulnerabilityMap(String beginCreateTime, String endCreateTime) {
        TreeList<SalesStatisticVo> list = new TreeList<>();
        //得到时间区间内的所有id
        List<Long> ids = sysUserMapper.selectids(beginCreateTime, endCreateTime);
        //遍历id
        for (Long id : ids) {
            SalesStatisticVo salesStatisticVo = new SalesStatisticVo();
            //得到每一个用户user_id,通过用户id得到用户名create_by
            String Create_by = sysUserMapper.selectNike_Name(id);
            salesStatisticVo.setCreate_by(Create_by);
            //得到每一个用户id,通过用户id得到部门id   得到部门名字
            Long deptId = sysUserMapper.selectDeptId(id);
            String deptName = deptMapper.selectDeptName(deptId);
            salesStatisticVo.setDeptName(deptName);
            //得到每一个用户id,得到商机数
            Integer nums = businessMapper.selectBusiness(id, beginCreateTime, endCreateTime);
            salesStatisticVo.setNum(nums);
            //线索数
            Integer count = sysUserMapper.selectCount(id, beginCreateTime, endCreateTime);
            if (nums.equals(0) || count.equals(0)) {
                salesStatisticVo.setRadio("0");
            } else {
                count = (nums * 100) / count;
                salesStatisticVo.setRadio(count + "");
            }
            list.add(salesStatisticVo);
        }
        return list;
    }
}
